<?php 

Class indexModel extends appModel
{
//获取文章的列表
	
	public function cat_art_list($cfg=array())
	{
		$sql="SELECT id,name,type FROM ".$this->table('category')." WHERE code='".$cfg['code']."'";
		$parent=$this->getRow($sql);
		$limit='';
		$where='';
		if($cfg['limit']!='')
		{
			$limit=" LIMIT ".$cfg['limit'];
		}
		if($cfg['flags']!='')
		{
			$where.=" AND instr(flags ,'".$cfg['flags']."')";
		}
		if($parent['id'])
		{
			$parent['name']=hy_dig_filter($parent['name']);
			if(REWRITE==1)
			{
				switch($parent['type'])
				{
					case 1:$parent['url']="/".$cfg['code']."/article/index.html";break;
					case 2:$parent['url']="/".$cfg['code']."/article/photo.html";break;
					case 3:$parent['url']="/".$cfg['code']."/article/page.html";break;
				}
			}
			else
			{
				switch($parent['type'])
				{
					case 1:$parent['url']="index.php?g=home&m=article&a=index&code=".$cfg['code'];break;
					case 2:$parent['url']="index.php?g=home&m=article&a=photo&code=".$cfg['code'];break;
					case 3:$parent['url']="index.php?g=home&m=article&a=page&code=".$cfg['code'];break;
	
				}
			}
			$cat_field=$this->getCatTree(array(),$parent['id']);
			$cat_field[]=$parent['id'];
			$sql="SELECT id,title,thumb,summary FROM ".$this->table('article')." WHERE cat_id in (".implode(',', $cat_field).")".$where." ORDER BY id ".$limit;
			$art=$this->getAll($sql);
			foreach($art as $k=>$v)
			{
				$art[$k]['summary']=str_replace(PHP_EOL, '<br>', hy_dig_filter($v['summary']));
				$art[$k]['title']=hy_dig_filter($v['title']);
				if(REWRITE==1)
				{
					$art[$k]['url']="/a/".$v['id'].".html";
				}
				else
				{
					$art[$k]['url']="/index.php?g=home&m=article&a=view&id=".$v['id'];
				}
			}
			$parent['article']=$art;
				
			//$art_list['title']=hy_dig_filter($art['title']);
				
		}
	
		return $parent;
	}
	//获取一篇文章的摘要
	public function cat_page($cfg=array())
	{
		$sql="SELECT id,name FROM ".$this->table('category')." WHERE code='".$cfg['code']."'";
		$parent=$this->getRow($sql);
		if($parent['id'])
		{
			$parent['name']=hy_dig_filter($parent['name']);
			if(REWRITE==1)
			{
				switch($parent['type'])
				{
					case 1:$parent['url']="/".$cfg['code']."/article/index.html";break;
					case 2:$parent['url']="/".$cfg['code']."/article/photo.html";break;
					case 3:$parent['url']="/".$cfg['code']."/article/page.html";break;
					 
				}
			}
			else
			{
				switch($parent['type'])
				{
					case 1:$parent['url']="index.php?g=home&m=article&a=index&code=".$cfg['code'];break;
					case 2:$parent['url']="index.php?g=home&m=article&a=photo&code=".$cfg['code'];break;
					case 3:$parent['url']="index.php?g=home&m=article&a=page&code=".$cfg['code'];break;
					 
				}
			}
			$sql="SELECT id,title,thumb,summary FROM ".$this->table('article')." WHERE cat_id=".$parent['id']." ORDER BY id LIMIT 1";
			$art=$this->getRow($sql);
			$parent['summary']=str_replace(PHP_EOL, '<br>', hy_dig_filter($art['summary']));
			//$art_list['title']=hy_dig_filter($art['title']);
			
		}
		
		return $parent;
	}
	public function  cat_child($cfg=array())
	{
		if($cfg['limit'])
		{
			$num=$cfg['limit'];
		}
		else 
		{
			$num=10;
		}
		$sql="SELECT id,name,type FROM ".$this->table('category')." WHERE code='".$cfg['code']."'";
		$parent=$this->getRow($sql);
	    if($parent['id'])
	    {
	    	$parent['name']=hy_dig_filter($parent['name']);
	    	if(REWRITE==1)
	    	{
	    		switch($parent['type'])
	    		{
	    			case 1:$parent['url']="/".$cfg['code']."/article/index.html";break;
	    			case 2:$parent['url']="/".$cfg['code']."/article/photo.html";break;
	    			case 3:$parent['url']="/".$cfg['code']."/article/page.html";break;
	    				
	    		}
	    	}
	    	else
	    	{
	    		switch($parent['type'])
	    		{
	    			case 1:$parent['url']="index.php?g=home&m=article&a=index&code=".$cfg['code'];break;
	    			case 2:$parent['url']="index.php?g=home&m=article&a=photo&code=".$cfg['code'];break;
	    			case 3:$parent['url']="index.php?g=home&m=article&a=page&code=".$cfg['code'];break;
	    				
	    		}
	    	}
	    	
	    	$sql="SELECT id,name,type,code FROM ".$this->table('category')." WHERE parent_id='".$parent['id']."' ORDER BY created LIMIT ".$num;
	    	$parent['child']=$this->getALL($sql);
	    	if($parent['child'])
	    	{
	    		foreach($parent['child'] as $k=>$v)
	    		{
	    			if(REWRITE==1)
	    			{
	    				switch($v['type'])
	    				{
	    					case 1:$parent['child'][$k]['url']="/".$v['code']."/article/index.html";break;
	    					case 2:$parent['child'][$k]['url']="/".$v['code']."/article/photo.html";break;
	    					case 3:$parent['child'][$k]['url']="/".$v['code']."/article/page.html";break;
	    				}
	    			}
	    			else
	    			{
	    				switch($v['type'])
	    				{
	    					case 1:$parent['child'][$k]['url']="index.php?g=home&m=article&a=index&code=".$v['code'];break;
	    					case 2:$parent['child'][$k]['url']="index.php?g=home&m=article&a=photo&code=".$v['code'];break;
	    					case 3:$parent['child'][$k]['url']="index.php?g=home&m=article&a=page&code=".$v['code'];break;
	    				}
	    			}
	    		}
	    	}
	    }
		return $parent;
	}
	public function  get_product_list($code,$num=10)
	{
		$sql="SELECT id FROM ".$this->table('category')." WHERE code='".$code."'";
		$id=$this->getOne($sql);
		
		$sql="SELECT id,title,thumb,created FROM ".$this->table('product')." WHERE cat_id=".$id." ORDER BY created LIMIT ".$num;
		$art_list=$this->getALL($sql);
	
		return $art_list;
	}
	public function  get_album_list($code,$num=10)
	{
		$art_list=array();
		
		$sql="SELECT id FROM ".$this->table('category')." WHERE code='".$code."'";
		$id=$this->getOne($sql);
		
		$sql="SELECT id,title,(SELECT small_pic FROM ".$this->table('album_photos')." as a WHERE a.id=b.album_cover) as cover,created FROM ".$this->table('albums')." as b WHERE cat_id=".$id." ORDER BY created";
		$temp=$this->getRow($sql);
		if(!empty($temp))
		{
		$art_list[]=$temp;
		}
		
		$sql="SELECT id FROM ".$this->table('category')." WHERE parent_id=".$id;
		$child_list=$this->getALL($sql);
		
		
		foreach($child_list as $k=>$v)
		{
			$sql="SELECT id,title,(SELECT small_pic FROM ".$this->table('album_photos')." as a WHERE a.id=b.album_cover) as cover,created FROM ".$this->table('albums')." as b WHERE cat_id=".$v['id'];
			$temp=$this->getRow($sql);
		    if(!empty($temp))
		   {
			$art_list[]=$temp;
		   }
		}
		
		return $art_list;
	}
}